package blog.controller;

import blog.entity.Link;
import blog.service.LinkService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Controller @RequestMapping("link")
public class LinkController {
    @Resource
    private LinkService linkService;
    @RequestMapping("")
    public String listLink(Map map){
        List<Link>linkList =  linkService.getLinkList();
        map.put("linkList",linkList);
        return "link/link-list";
    }
    //删除一个链接
    @RequestMapping("/delete/{id}")
    public String deleteLink(@PathVariable("id") Integer id){
        linkService.deleteLinkById(id);
        return "forward:/link";
    }
    @RequestMapping(value = "/edit/{id}",method = RequestMethod.GET)
    public String forEditLink(@PathVariable("id") Integer id,Map map){
        Link link = linkService.getLinkById(id);
        map.put("link",link);
        return "link/link-editor";
    }
    @RequestMapping(value = "/edit",method = RequestMethod.POST)
    public String editLink(Link link,Map map){
        linkService.updateLink(link);
        map.put("msg","修改成功");
        return "forward:/link";
    }
    @RequestMapping("/insert")
    public String gotoInsert(Map map){
        List<Link> linkList = linkService.getLinkList();
        map.put("linkList",linkList);
        return "link/link-add";
    }
    @RequestMapping("/addLink")
    public String addLink(Link link,Map map){
        link.setLinkCreateTime(new Date());
        link.setLinkUpdateTime(new Date());
        link.setLinkStatus(1);
        linkService.addLink(link);
        return "forward:/link";
    }
}
